package common

import (
	"fmt"
	"log"
	"path/filepath"
	"runtime"

	"github.com/go-ini/ini"

	"gitee.com/goframe/lt-collector/libs/abio"
	"gitee.com/goframe/lt-collector/libs/libce"
	"gitee.com/goframe/lt-collector/libs/openssl"
	"gitee.com/goframe/lt-collector/libs/orma"
)

const (
	Version = "v0.0.1"
)

var (
	Server = func() string {
		switch runtime.GOOS {
		case "linux":
			return "ty-collector"
		case "darwin":
			return "mac-ty-collector"
		default:
			log.Fatalf("不支持的操作系统%s\n", runtime.GOOS)
		}
		return ""
	}()
)

var (
	DefConf = Conf{
		Web: Web{
			Domain:    "*",
			Listen:    "39200",
			Secret:    openssl.RandomString(32),
			CacheType: "redis",
		},
		Redis: Redis{
			Addr:     "127.0.0.1:6379",
			Password: "",
			DB:       12,
		},
		Pull: PullBind{
			PullDomain: "yh8.in:39001",
			SoftName:   Server,
			Branch:     "dev",
			Domain:     "127.0.0.1:39200",
		},
		DataBase: orma.DB{
			Type:        "mysql",
			Host:        "127.0.0.1",
			Port:        3306,
			User:        "root",
			Password:    "123",
			DB:          "ty_collector",
			Cache:       false,
			ShowSQL:     false,
			MaxIdleConn: 10,
			MaxConn:     100,
		},
	}
)

type (
	Conf struct {
		Web      Web      `json:"web" ini:"web" comment:"服务配置"`
		Redis    Redis    `json:"redis" ini:"redis" comment:"redis 配置"`
		DataBase orma.DB  `json:"data_base" ini:"data_base" comment:"数据库配置"`
		Pull     PullBind `json:"pull" ini:"pull" comment:"更新配置"`
	}

	Web struct {
		Domain    string `json:"domain" ini:"domain" comment:"绑定域名;逗号分隔,*不限制"`
		Listen    string `json:"listen" ini:"listen" comment:"监听地址"`
		Secret    string `json:"secret" ini:"secret" comment:"加密秘钥,不能修改"`
		CacheType string `json:"cache_type" ini:"cache_type" comment:"缓存类型;memory or redis"`
	}

	Redis struct {
		Addr     string `json:"addr" ini:"addr" comment:"redis server address;example:127.0.0.1:6379"`
		Password string `json:"password" ini:"password"`
		DB       int    `json:"db" ini:"db"`
	}

	PullBind struct {
		PullDomain string `json:"pull_domain" ini:"pull_domain"`
		SoftName   string `json:"soft_name" ini:"soft_name"`
		Branch     string `json:"branch" ini:"branch"`
		Domain     string `json:"domain" ini:"domain"`
	}
)

func (c Conf) GetCache() (cache libce.CEInterface) {
	if c.Web.CacheType == "redis" {
		cache = libce.NewRedis(c.Redis.getSdn(), Server)
	} else {
		cache = libce.NewMemoryCache()
	}
	return
}

func (r Redis) getSdn() string {
	return fmt.Sprintf(`{"Addr": "%s","Password": "%s","DB": %d}`, r.Addr, r.Password, r.DB)
}

func LoadConfig() {
	var err error
	defer func() {
		if err != nil {
			log.Fatalf("配置加载失败:%s\n", err.Error())
		}
	}()

	var iniFile *ini.File
	var fileName = filepath.Join(abio.SelfDir(), "conf", "conf.ini")
	iniFile, err = ini.Load(fileName)
	if err != nil {
		if err = abio.TouchFile(fileName); err != nil {
			return
		}
		iniFile = ini.Empty()
	} else {
		err = iniFile.MapTo(&DefConf)
	}
	if err != nil {
		return
	}
	if err = iniFile.ReflectFrom(&DefConf); err != nil {
		return
	}
	err = iniFile.SaveTo(fileName)
}
